import time
import os
import psutil
from flask import jsonify

def main_compkey(seed_keyword,thousand):

    start_time = time.time()

    # 主程序
    # seed_keyword = "大学"
    data_file = 'data\每'+str(thousand)+'k条\data_'+str(thousand)+'k.txt'
    related_keywords = []
    all_search_volume = 0
    related_keyword_search_volume = {}
    related_keyword_weights = {}
    sorted_related_keyword_weights = {}

    # print("种子关键词：", seed_keyword)

    # 1. 读取txt数据文件并获取与种子关键字相关的中介关键字
    with open(data_file, 'r', encoding='utf-8') as file:
        for line in file:
            keywords = line.strip().split('\t')
            if seed_keyword in keywords:
                all_search_volume += 1
                for keyword_demo in keywords:  # 排除只有一个字的词语
                    if len(keyword_demo) <= 1:
                        keywords.remove(keyword_demo)
                related_keywords.extend(keywords)
                # 2. 统计包含种子关键字和中介关键字的查询搜索量
                for keyword in keywords:
                    if keyword != seed_keyword and keyword in related_keywords:
                        related_keyword_search_volume[keyword] = related_keyword_search_volume.get(keyword, 0) + 1
    file.close()
    related_keywords = set(related_keywords)
    if related_keywords.__contains__(seed_keyword):
        related_keywords.remove(seed_keyword)
    # print("与种子关键字相关的中介关键字：", related_keywords)
    # print("包含种子关键字的查询搜索量：", all_search_volume)

    # 3. 计算中介关键字的权重
    for keyword in related_keywords:
        related_keyword_weights[keyword] = round(related_keyword_search_volume[keyword]/all_search_volume,3)
    # print("包含种子关键字和中介关键字的查询搜索量：", related_keyword_search_volume)
    # print("中介关键字的权重：", related_keyword_weights)

    # 对中介关键词的权重进行排序，取前十个
    sorted_related_keyword_weights = sorted(related_keyword_weights.items(),key=lambda x:x[1],reverse=True)[:10]
    sorted_related_keywords = [item[0] for item in sorted_related_keyword_weights]
    # print("排序后的前十个中介关键字：", sorted_related_keywords)

    # 4. 确定竞争性关键字候选集合
    competitive_keywords = {}
    all_competitive_keywords = []
    for related_keyword in sorted_related_keywords:
        competitive_keywords[related_keyword] = []
    with open(data_file, 'r', encoding='utf-8') as file:
        for line in file:
            keywords = line.strip().split('\t')
            for related_keyword in sorted_related_keywords:
                if seed_keyword not in keywords and related_keyword in keywords:
                    for related_keyword1 in sorted_related_keywords:
                        if related_keyword1 in keywords: # 竞争性关键词中排除中介关键词
                            keywords.remove(related_keyword1)
                        for related_keyword_demo in keywords: # 竞争性关键词中排除只有一个文字的词
                            if len(related_keyword_demo) <= 1:
                                keywords.remove(related_keyword_demo)
                    all_competitive_keywords.extend(keywords)
                    competitive_keywords[related_keyword].extend(keywords)
                    competitive_keywords[related_keyword] = list(set(competitive_keywords[related_keyword]))
    all_competitive_keywords = set(all_competitive_keywords)
    # print("每个中介关键词的竞争性关键字候选集合：", competitive_keywords)
    # print("所有竞争性关键字候选集合：", all_competitive_keywords)
    file.close()

    # 5. 计算竞争关键字的竞争度
    competition_scores = {}
    ka = {}     # 任意关键词与中介关键词出现次数
    a = {}      # 中介关键词出现次数
    sa = related_keyword_search_volume     # 种子关键词与中介关键词出现次数
    s = all_search_volume   # 种子关键词的出现次数
    #   关键字之间的竞争度Comp_i = ka/(a-sa)
    for related_keyword in sorted_related_keywords:
        a[related_keyword] = 0
        ka[related_keyword] = {}
        competition_scores[related_keyword] = {}
        for casual_keyword in competitive_keywords[related_keyword]:
            if related_keyword!=casual_keyword:
                ka[related_keyword][casual_keyword] = 0
                competition_scores[related_keyword][casual_keyword] = 0

    with open(data_file, 'r', encoding='utf-8') as file:
        for line in file:
            keywords = line.strip().split('\t')
            for related_keyword in sorted_related_keywords:
                if related_keyword in keywords:
                    a[related_keyword] += 1
                for casual_keyword in competitive_keywords[related_keyword]:
                    if related_keyword!=casual_keyword and related_keyword in keywords and casual_keyword in keywords:
                        ka[related_keyword][casual_keyword] += 1      
    # print("ka",ka)
    # print("a",a)
    # print("sa",sa)
    for related_keyword in sorted_related_keywords:
        for casual_keyword in competitive_keywords[related_keyword]:
            if casual_keyword != related_keyword:
                if a[related_keyword] == sa[related_keyword]:
                    competition_scores[related_keyword][casual_keyword] = 1
                else:
                    competition_scores[related_keyword][casual_keyword] = round(ka[related_keyword][casual_keyword]/(a[related_keyword]-sa[related_keyword]),3)

    # 根据竞争度从高到低排序，对每个中介关键词取前五个竞争关键词
    sorted_competition_keywords = {key: dict(sorted(value.items(), key=lambda item: item[1], reverse=True)[:5]) for key, value in competition_scores.items()}
    # print("竞争关键字的竞争度：", sorted_competition_keywords)

    # 竞争关键词与种子关键词之间的竞争度 = Σ{(sa_i/s)*Comp_i
    # 计算权重*Comp的累和
    compitition_result = {}
    for related_keyword in sorted_related_keywords:
        for casual_keyword in sorted_competition_keywords[related_keyword]:
            compitition_result[casual_keyword] = 0
    for related_keyword in sorted_related_keywords:
        wak = sa[related_keyword]/s
        for casual_keyword in sorted_competition_keywords[related_keyword]:
            compitition_result[casual_keyword] += wak*competition_scores[related_keyword][casual_keyword]
    for related_keyword in sorted_related_keywords:
        for casual_keyword in sorted_competition_keywords[related_keyword]:
            compitition_result[casual_keyword] = round(compitition_result[casual_keyword],3)
    compitition_result = sorted(compitition_result.items(), key=lambda x: x[1], reverse=True) # [:10]
    # print(compitition_result)

    # 整理数据输出格式
    result = {}
    result['seed_keyword'] = seed_keyword
    result['seed_keyword_volume'] = all_search_volume

    # result['mid_keywords'] = {}
    # for print_related_keyword in competition_scores:
    #     result['mid_keywords'][print_related_keyword] = {}
    #     result['mid_keywords'][print_related_keyword]['volume'] = related_keyword_weights[print_related_keyword]
    #     result['mid_keywords'][print_related_keyword]['compkeys'] = sorted_competition_keywords[print_related_keyword]
            
    # result['mid_keywords_final'] = sorted_related_keyword_weights
    # result['compkeys_final'] = compitition_result


    result['mid_keywords'] = []
    for print_related_keyword in competition_scores:
        result['mid_keywords'].append({
                'midkeyname': print_related_keyword,
                'midkeyvalue': related_keyword_weights[print_related_keyword],
                'compkeys': sorted_competition_keywords[print_related_keyword]
            }) 
    result['mid_keywords_final'] = sorted_related_keyword_weights
    result['compkeys_final'] = compitition_result


    # print("-----------------------------------------------------------------对于每个中介关键词的竞争关键词排序输出-------------------------------------------------------------------------")
    # for print_related_keyword in competition_scores:
    #     casual_keywords = competition_scores[print_related_keyword]
    #     print("(种子关键词)",seed_keyword,":", all_search_volume ,"\t(中介关键词)",print_related_keyword,":",related_keyword_weights[print_related_keyword],"\t(竞争关键词)",sorted_competition_keywords[print_related_keyword])
        
    
    # print("\n-----------------------------------------------------------------乘以中介关键词权重后的竞争关键词排序输出-------------------------------------------------------------------------")
    # print("(种子关键词)",seed_keyword,":", all_search_volume ,"\n(中介关键词)",sorted_related_keyword_weights,"\n(竞争关键词)",compitition_result)


    # end_time = time.time()
    # execution_time = end_time - start_time
    # print(f"程序运行时间: {execution_time}秒")

    # process = psutil.Process(os.getpid())
    # memory_usage = process.memory_info().rss
    # print(f"程序运行内存: {memory_usage / 1024 / 1024} MB")

    # print(result)
    
    return result